import Mock from "mockjs";
// import { ElMessage } from "element-plus";
// import "element-plus/theme-chalk/el-message.css";
interface User {
  username: string;
  password: string;
}
const userList: User[] = [
  {
    username: "admin",
    password: "123456",
  },
];
// 登录接口
Mock.mock("/api/login", "post", function (options: any) {
  let params = JSON.parse(options.body);
  const { username, password } = params;
  // console.log(username, password);
  if (username === "admin" && password === "123456") {
    return { code: 200, success: true, message: "登录成功", data: { token: Mock.Random.guid() } };
  } else {
    return { code: 400, success: false, message: "登录失败,用户名或密码错误" };
  }
});
// 注册接口
Mock.mock("/api/regin", "post", (options: any) => {
  // const { username, password } = JSON.parse(options.body);
  // if (username && password) {
  //   userList.forEach((item) => {
  //     if (item.username === username && item.password === password) {
  //       return { code: 400, success: false, message: "用户名已存在,请重新注册" };
  //     }
  //   });
  //   userList.push({ username, password });
  //   return { code: 200, success: true, message: "注册成功" };
  // } else {
  //   return { code: 400, success: false, message: "注册失败" };
  // }
  const { userName, password } = JSON.parse(options.body);

  //
  userList.forEach((item) => {
    if (item.username === userName && item.password === password) {
      return { code: 400, success: false, message: "用户名已存在,请重新注册" };
    }
    //
    else {
      userList.push({ username: userName, password: password });
      return { code: 200, success: true, message: "注册成功" };
    }
  });
});
